package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.github.pagehelper.PageHelper;
import com.jlkf.dao.CommonlyUsedAddressMapper;
import com.jlkf.pojo.CommonlyUsedAddress;
import com.jlkf.service.CommonlyUsedAddressService;
import com.jlkf.service.util.RegexUtil;
import com.jlkf.service.util.StringTools;
import com.platform.server.response.pojo.ReturnData;
@Service
public class CommonlyUsedAddressServiceImpl implements CommonlyUsedAddressService{

	@Autowired
	private  CommonlyUsedAddressMapper commonlyUsedAddressMapper; 
	
	@Override
	public ReturnData getCommonlyUsedAddress(Long uId,Long classificationId,Integer pageNo,Integer pageSize) {
		Map<String,Object> map = new HashMap<>();
		map.put("uId", uId);
		map.put("type", classificationId);
		if(RegexUtil.isNull(pageNo)){
			pageNo=1;
		}
		if(RegexUtil.isNull(pageSize)){
			pageSize=15;
		}
		try {
			PageHelper.startPage(pageNo, pageSize);
			List<CommonlyUsedAddress> list=commonlyUsedAddressMapper.getCommonlyUsedAddress(map);
			return new ReturnData(list,200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	
	@Override
	public ReturnData setCommonAddress(Long uId, Integer type, String address,String placeId,String fullAddress) {
		if(RegexUtil.isNull(type)){
			return new ReturnData(201, "The type cannot be empty！");
		}
		if(StringTools.isEmpty(address)){
			return new ReturnData(201, "The address cannot be empty");
		}
		if(RegexUtil.isNull(uId)){
			return new ReturnData(201, "The uId cannot be empty");
		}
		try {
			CommonlyUsedAddress cua = new CommonlyUsedAddress();
			cua.setAddress(address);
			cua.setCreateTime(new Date());
			cua.setType(type);
			cua.setuId(uId);
			cua.setPlaceId(placeId);
			cua.setFullAddress(fullAddress);
			int num =commonlyUsedAddressMapper.insertSelective(cua);
			if(num==0){
				return new ReturnData(202,"Setup Failed！");
			}
			return new ReturnData(200,"Setup Completed！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}


	@Override
	public ReturnData deleteCommonAddress(Long uId, Integer type) {
		Map<String,Object> map = new HashMap<>();
		map.put("uId", uId);
		map.put("type", type);
		boolean flag=true;
		try {
			List<CommonlyUsedAddress> list=commonlyUsedAddressMapper.getCommonlyUsedAddress(map);
			for(CommonlyUsedAddress cua:list){
				int num=commonlyUsedAddressMapper.deleteByPrimaryKey(cua.getId());
			   if(num==0){
				   flag=false;
				   break;
			   }
			}
			if(!flag){
				TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			return new ReturnData(209,"delete fail");
			}
			return new ReturnData(200,"delete success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	
}
